package com.junsi.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.Notice;
import com.junsi.bean.Staff;
import com.junsi.service.NoticeSerivce;
import com.junsi.util.InteractiveUtil;
import com.junsi.util.PageUtil;

@Controller
@RequestMapping(value = "/notice")
public class NoticeController {
	
	@Autowired
	private NoticeSerivce noticeSerivce;
	
	/**
	 * 分页查询所有公告信息
	 * @return
	 */
	@RequestMapping(value="/page")
	public ModelAndView page(PageUtil page, HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView();
		page.setPageSize(100);
		page.setPageNum(1);
		Integer number = noticeSerivce.findByNumber();
		if (number != null) {
			page.setRowCount(number);
			List<Notice> list = noticeSerivce.pageFind(page.getEndIndex(), page.getBeginIndex());
			modelAndView.addObject("list", list);
		}
		modelAndView.setViewName("forward:/html/yifa.jsp");
		return modelAndView;
	}
	
	
	/**
	 * 新增
	 */
	@RequestMapping("/save")
	public void save(Notice notice, HttpServletResponse response, HttpServletRequest request){
		if (notice != null) {
			notice.setNotice_releaseDate(new Date());
			boolean result = noticeSerivce.save(notice);
			if (result) {
				InteractiveUtil.WriteStringForHtml("新增成功", response);
			}else{
				InteractiveUtil.WriteStringForHtml("新增失败", response);
			}
		}else{
			InteractiveUtil.WriteStringForHtml("新增失败", response);
		}
				
	}
		
	
	/**
	 * 修改
	 * @return
	 */
	@RequestMapping("/update")
	public void update(Notice notice, HttpServletResponse response){
		if (notice != null && notice.getNotice_id() != null) {
			boolean result = noticeSerivce.update(notice);
			if(result){
				InteractiveUtil.WriteStringForHtml("修改成功", response);
			}else{
				InteractiveUtil.WriteStringForHtml("修改失败", response);
			}
		}else{
			InteractiveUtil.WriteStringForHtml("修改失败", response);
		}
	}
	
				
	/**
	 * 单个ID删除
	 * @return
	 */
	@RequestMapping("deleteById")
	public void deleteById(Integer id, HttpServletResponse response){
		if (id != null) {
			boolean result = noticeSerivce.delete(id);
			if (result) {
				InteractiveUtil.WriteStringForHtml("删除成功", response);
			}else {
				InteractiveUtil.WriteStringForHtml("删除失败", response);
			}
		}else {
			InteractiveUtil.WriteStringForHtml("删除失败", response);
		}
	}
	
	
	/**
	 * 根据发布时间查询公告信息
	 * @return
	 */
	@RequestMapping("/findByDate")
	public ModelAndView findByDate(String notice_releaseDate, HttpServletResponse response){
		ModelAndView modelAndView = new ModelAndView();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = simpleDateFormat.parse(notice_releaseDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<Notice> list = noticeSerivce.findByDate(date);
		modelAndView.addObject("list", list);
		modelAndView.setViewName("forward:/html/yifa.jsp");
		return modelAndView;	
	}

	
	/**
	 * 根据发布时间降序查询所有公告信息
	 * @return
	 */
	@RequestMapping("/findByAll")
	public ModelAndView findByAll(HttpServletResponse response){
		ModelAndView modelAndView = new ModelAndView();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		List<Map<String, String>> array = new ArrayList<Map<String, String>>();		
		List<Notice> list = noticeSerivce.findByAll();
		for(Notice n: list){
			Map<String, String> map = new HashMap<String, String>();
			String release = null;
			release = simpleDateFormat.format(n.getNotice_releaseDate());
			map.put("notice_releaseDate", release);
			map.put("notice_text", n.getNotice_text());
			array.add(map);
		}
		modelAndView.addObject("list", array);
		modelAndView.setViewName("forward:/html/index.jsp");
		return modelAndView;		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	private List<Map<String, String>> ArrayList() {
		// TODO Auto-generated method stub
		return null;
	}


	/**
	 * 批量删除
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteManyId")
	@ResponseBody
	public void deleteManyId(String ids, HttpServletResponse response) {
		if (ids != null) {
			boolean result = noticeSerivce.deletes(ids);
			if (result) {
				InteractiveUtil.WriteStringForHtml("删除成功", response);
			}else {
				InteractiveUtil.WriteStringForHtml("删除失败", response);
			}
		}else {
			InteractiveUtil.WriteStringForHtml("删除失败", response);
		}
	}

	
	
	
}
